<?php
include_once '../Login/databaseconnection.php';

$Started_year=$_POST['Started_year'];
$Institute=$_POST['Institute'];
$Training_Id=$_POST['Training_Id'];
$New_Training=$_POST['New_Training'];
if($New_Training!=""&&$Training_Id!="Select One")
{
$_SESSION['error']="Please select the programme or type new!";
}
else
{
   date_default_timezone_set("Europe/Paris");
   $t_d=date("Y-m-d");
if($New_Training!="")
{

$maxid=mysql_query("SELECT max(Training_Id) as maxId FROM training");

while($rowid=mysql_fetch_array($maxid))
{
$m_Id=$rowid['maxId'];
if($m_Id=="")
{
$Training_Id="T0001";
}
else
{
 $inum=substr("$m_Id",1);
$inn=($inum+1);
$Training_Id='T';
if(strlen($inn)<strlen($inum))
{
for($i=0;$i<(strlen($inum)-strlen($inn));$i++)
{
$Training_Id=$Training_Id.'0';
}
$Training_Id=$Training_Id.$inn;
}
else
{
$Training_Id=$Training_Id.$inn;
}
}
}
$sql_tra=mysql_query("INSERT INTO training(Training_Id,Training_Name,Last_Updated_Date)values('$Training_Id','$New_Training','$t_d')");
}
$Teacher_NIC=$_POST['Teacher_NIC'];
$Duration=$_POST['Duration'];
if($Duration=="")
{
$Dura="0year";
}
if($Duration!="")
{
$chars = str_split( $Duration );
  $i=0;
while($i<strlen($Duration))
{
$c=$chars[$i]; 
if($c=="y"||$c=="Y")
{
$st=$i;
break;
}
else
{
$i++;
}
}
if($st>0)
{
$Dur=substr($Duration,0,$st);
$Dur_chars=str_split($Dur);
for($i=0;$i<count($Dur_chars);$i++)
{
if($Dur_chars[$i]!="0"&&$Dur_chars[$i]!="1"&&$Dur_chars[$i]!="2"&&$Dur_chars[$i]!="3"&&$Dur_chars[$i]!="4"&&$Dur_chars[$i]!="5"&&$Dur_chars[$i]!="6"&&$Dur_chars[$i]!="7"&&$Dur_chars[$i]!="8"&&$Dur_chars[$i]!="9")
{
$Dur="0";
}
}
}
if($st==0)
{
$Dur="0";
}
if($st=="")
{
$Dur_chars=str_split($Duration);
for($i=0;$i<count($Dur_chars);$i++)
{
if($Dur_chars[$i]!="0"&&$Dur_chars[$i]!="1"&&$Dur_chars[$i]!="2"&&$Dur_chars[$i]!="3"&&$Dur_chars[$i]!="4"&&$Dur_chars[$i]!="5"&&$Dur_chars[$i]!="6"&&$Dur_chars[$i]!="7"&&$Dur_chars[$i]!="8"&&$Dur_chars[$i]!="9")
{
$Dur="0";
}
else
{
$Dur=$Duration;
}
}
}

if((int) $Dur>1)
{
$Dura=$Dur."years";
}
if((int) $Dur<=1)
{
$Dura=$Dur."year";
}
}
$_SESSION['Training_Id']=$Training_Id;
$_SESSION['Teacher_NIC']=$Teacher_NIC;
$_SESSION['Duration']=$Dura;
   
if(($Training_Id!=""||$New_Training!="")&&$Teacher_NIC!="Select One"&&$Duration!=""&&$Institute!=""&&$Started_year!="")
	{
	if($_SESSION['Access_Type']=="Teacher in Charge")
{
   $NIC_Number=$_SESSION['NIC_Number'];
   $get_Sch=mysql_query("SELECT * FROM teacher WHERE T_NIC_Number='$NIC_Number'");
   if($rowsch=mysql_fetch_array($get_Sch))
{
   $Own_School_Id=$rowsch['School_Id'];
  
   if($School_Id==$Own_School_Id)
	{
	
	$sql=mysql_query("INSERT INTO training_details(Year,Institute,Training_Id,Teacher_NIC,Duration,Last_Updated_Date) VALUES('$Started_year','$Institute','$Training_Id','$Teacher_NIC','$Dura','$t_d')");
if($sql)
  {
$_SESSION['error']="This record is successfully registered!";
$_SESSION['OK']="OK";
$_SESSION['Training_Id']=$Training_Id;
$_SESSION['School_Id']=$School_Id;
$_SESSION['Date']=$Date;
  }

  if(mysql_affected_rows()==0)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
	}
	else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
	}
	}
	else if($_SESSION['Access_Type']=="Administrator")
{
$sql=mysql_query("INSERT INTO training_details(Year,Institute,Training_Id,Teacher_NIC,Duration,Last_Updated_Date) VALUES('$Started_year','$Institute','$Training_Id','$Teacher_NIC','$Dura','$t_d')");
if($sql)
  {
$_SESSION['error']="This record is successfully registered!";
$_SESSION['OK']="OK";
$_SESSION['Training_Id']=$Training_Id;
$_SESSION['School_Id']=$School_Id;
$_SESSION['Date']=$Date;
  }

  if(mysql_affected_rows()==0)
  {
$_SESSION['error']="Sorry unable to register this record!";
  }
  }
  else
	   {
	   $_SESSION['errornotallo']="Sorry you are not allowed to do this!";
	   }
  }
  if(($Training_Id==""&&$New_Training=="")||$Teacher_NIC=="Select One"||$Duration==""||$Institute==""||$Started_year=="")

	{
	$_SESSION['errorin']="All details are required please fill all!";
  }
  }
?>